package com.doform.android.xianggucheng;

import android.app.Application;
import android.content.Context;

import com.doform.android.xianggucheng.activity.BaseActivity;
import com.doform.android.xianggucheng.help.AppHelp;
import com.doform.android.xianggucheng.service.PushService;
import com.doform.android.xianggucheng.service.TSIntentService;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.igexin.sdk.PushManager;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import static com.orhanobut.logger.Logger.init;


/**
 * Created by 002 on 2017/3/14.
 */

public class IApplication extends Application {

    private static IApplication instance;
    public static final String apen = "apen";
    private static List<BaseActivity> activities = new ArrayList<>();

    @Override
    public void onCreate() {
        super.onCreate();

        instance = this;

        initSDK();

        AppHelp.getInstance().init(this);

        init(apen);

        initHttp();

        initSmartRefresh();

    }
    private void initSmartRefresh() {

        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                return new MaterialHeader(context);//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                return new ClassicsFooter(context);//指定为经典Footer，默认是 BallPulseFooter
            }
        });

    }
    private void initSDK() {

        SpeechUtility.createUtility(this, SpeechConstant.APPID +"=5919723b");//科大讯飞

        PushManager.getInstance().initialize(this, PushService.class);
        // com.getui.demo.IntentService 为第三方自定义的推送服务事件接收类
        PushManager.getInstance().registerPushIntentService(this, TSIntentService.class);

    }

    private void initHttp() {
//        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
//                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
//                .readTimeout(10000L, TimeUnit.MILLISECONDS)
//                其他配置
//                .build();
//
//        OkHttpUtils.initClient(okHttpClient);
    }

    public static IApplication getInstance() {
        if (instance == null){
            instance = new IApplication();
        }
        return instance;
    }

    // 添加Activity到容器中
    public void addActivity(BaseActivity activity) {
        if (!activities.contains(activity))
            activities.add(activity);
    }

    public List<BaseActivity> getActivities(){
        return activities;
    }

    // 遍历所有Activity并finish
    public void destory() {
        for (BaseActivity activity : activities) {
            (activity).finish();
        }

        PushManager.getInstance().turnOffPush(this);

        MobclickAgent.onKillProcess(this);
    }

}
